package com.gopay.bis.dao.payconfig.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.gopay.bis.dao.payconfig.BankBindCardDAO;
import com.gopay.common.base.dao.impl.BaseDAOImpl;
import com.gopay.common.domain.bis.BisBankBindCardInfo;
import com.gopay.common.util.DateUtils;

@Repository("bankBindCardDAO")
public class BankBindCardDAOImpl extends BaseDAOImpl<BisBankBindCardInfo, String> implements BankBindCardDAO {

    @Override
    public void insertBankBindCard(BisBankBindCardInfo bindCard) {
        Date curDate = DateUtils.getSystemDate();
        bindCard.setCreated(curDate);
        bindCard.setLastUpdTs(curDate);
        this.getHibernateTemplate().save(bindCard);
        
    }
    
    @Override
    public int updateBindCard(BisBankBindCardInfo bindCard) {
        String hql = "update BisBankBindCardInfo set bindId= :bindId,"
                + "bindValid= :bindValid,phone= :phone,lastUpdTs= :lastUpdTs where cardNum= :cardNum";

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("cardNum", bindCard.getCardNum());
        params.put("bindId", bindCard.getBindId());
        params.put("bindValid", bindCard.getBindValid());
        params.put("phone", bindCard.getPhone());
        Date curDate = DateUtils.getSystemDate();
        params.put("lastUpdTs", curDate);
        int result = this.updateByHql(hql.toString(), params);
        return result;
    }

    @Override
    public BisBankBindCardInfo getBindCard(String userId, String cardNo) {
        String hql = " from BisBankBindCardInfo where userId = :userId and cardNum = :cardNum";
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("cardNum", cardNo);
        params.put("userId", userId);
        List<BisBankBindCardInfo> list = findByHql(hql, params);

        if (null == list || list.isEmpty()) {
            return null;
        }
        return (BisBankBindCardInfo) list.get(0);
    }

}
